package com.example.admin.xxxxxx;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements ListView.OnItemClickListener{
    Socket socket;
    ListView listView;
    OutputStream outputStream;
    InputStream inputStream;
    public static String IP="45.77.24.86";
    List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.list);
        listView.setOnItemClickListener(this);
       new getitem().execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent=new Intent(this,condition.class);
        intent.putExtra("ip",list.get(position));
        startActivity(intent);

    }


    class getitem extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("type", "list");
                jsonObject.put("value1", "1");
                jsonObject.put("value2", "2");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            String tr = jsonObject.toString();
            Log.e("A",tr);

            try {
                String str = null;
                socket = new Socket(MainActivity.IP, 3001);

                outputStream=socket.getOutputStream();

                outputStream.write(tr.getBytes());
                outputStream.flush();
                socket.shutdownOutput();

                inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                str = bufferedReader.readLine();

                Log.e("S",str);
                try {
                    JSONArray jsonArray = new JSONArray(str);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        list.add(jsonArray.getString(i));
                    }





                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
            listView.setAdapter(arrayAdapter);
        }
    }









}